package filesystem3;
//http://www.drdobbs.com/jvm/creating-and-modifying-xml-in-java/240150782
import java.io.File;

import javax.swing.filechooser.FileSystemView;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FilePermission;



import org.w3c.dom.Document;
import org.w3c.dom.Element;


//public void fileSystemView (FileSystemView view){
	
//}

public class XMLWriter{
	String fileName = "null";
	private String dir = this.dir;
	//default constructor
	XMLWriter (){
		
	}
	//will be called by FileViewerWindow
	XMLWriter (FileSystemView view){
		
	}
	//adding an element to XML

	public String getExtension(String fileName) {
		this.fileName = fileName;
		String extension = "null";
		int period = fileName.lastIndexOf('.');
		if (period > 0) {
			extension = fileName.substring(period + 1);
		}
		return extension;
	}//end getExtension
	
	public void add(FileSystemView file){
		
	}
	
	public void add(File file){

		
	}

	//public boolean lookup(){
		//if entry exists return true
	    //else return false
	//	if ()
	//	else()	
	//}

//	public boolean lookup(){
//		//if entry exists return 
//	    //else return false
//	}

//	public void read(){
//		
//	}
	
	public void CreateXMLFile(FileSystemView view) {
		
		try {
			

		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		
		// root elements
				Document doc = docBuilder.newDocument();
				Element rootElement = doc.createElement("fileSystem");
				doc.appendChild(rootElement);
				
				// file type
				Element fileType = doc.createElement("fileNotes");
				rootElement.appendChild(fileType);
				
				// setting attribute for filetype
				fileType.setAttribute("id", "1");
				
				// jpeg elements
				//call add method

				//add("Phone", "123-456-7890", exec );
				//addNode("Phone", "123-456-7890", exec );

//get the file extension path
				String ext = getExtension(fileName);
				//Element details = doc.createElement("fileNotes");
				
				Element details = doc.createElement("fileNotes");
				details.appendChild(doc.createTextNode("100000"));
				fileType.appendChild(details);
				
				Element details1 = doc.createElement("file1Notes");
				details1.appendChild(doc.createTextNode("100000"));
				fileType.appendChild(details1);
				
				Element details2 = doc.createElement("file1Notes");
				details2.appendChild(doc.createTextNode("100000"));
				fileType.appendChild(details2);
				
				
				
				
				// write the content into xml file
				TransformerFactory transformerFactory = TransformerFactory.newInstance();
				Transformer transformer = transformerFactory.newTransformer();
				DOMSource source = new DOMSource(doc);
				String x = view.getHomeDirectory().toString();
				StreamResult result = new StreamResult(new File(x +"\\database.xml"));
				
				transformer.transform(source, result);
				 
				System.out.println("File saved!");
				
				
		 } catch (ParserConfigurationException pce) {
				pce.printStackTrace();
			  } catch (TransformerException tfe) {
				tfe.printStackTrace();
			  }
			}
		}